﻿using System;
using System.Collections.Generic;
using System.Data;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    class MobGroupTable
    {
        private static Logger _log = Logger.getLogger( typeof( MobGroupTable ).FullName );

        private static MobGroupTable _instance;

        private Dictionary<int, L1MobGroup> _mobGroupIndex = new Dictionary<int, L1MobGroup>();

        public static MobGroupTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new MobGroupTable();
            }
            return _instance;
        }

        private MobGroupTable()
        {
            String sqlstr = "SELECT * FROM mobgroup";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    int mobGroupId = SqlUtils.toInt( item["id"] );
                    bool isRemoveGroup = Convert.ToBoolean( item["remove_group_if_leader_die"] );
                    int leaderId = SqlUtils.toInt( item["leader_id"] );
                    List<L1NpcCount> minions = new List<L1NpcCount>();//Lists.newArrayList();
                    for ( int i = 1; i <= 7; i++ )
                    {
                        int id = SqlUtils.toInt( item["minion" + i + "_id"] );
                        int count = SqlUtils.toInt( item["minion" + i + "_count"] );
                        minions.Add( new L1NpcCount( id, count ) );
                    }
                    L1MobGroup mobGroup = new L1MobGroup( mobGroupId, leaderId, minions, isRemoveGroup );
                    _mobGroupIndex[mobGroupId] = mobGroup;
                }
            }
            _log.config( "MOBグループリスト " + _mobGroupIndex.Count + "件ロード" );
        }

        public L1MobGroup getTemplate( int mobGroupId )
        {
            L1MobGroup ret;
            _mobGroupIndex.TryGetValue( mobGroupId, out ret );
            return ret;
        }
    }
}
